﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Model;
using ZBJF.Warranty.Model.ViewModel.Product;

namespace ZBJF.Warranty.BLL
{
    public partial class AccessoriesTypeService
    {
        #region 根据主键ID获取配件类型 + AccessoriesType LoadByID(int id)
        public AccessoriesType LoadByID(int id)
        {
            return LoadEntities(a => a.ID == id).FirstOrDefault();
        }
        #endregion

        #region 判断配件类型是否存在+bool IsExist(string accessoriestName, int id = 0)
        /// <summary>
        /// 判断配件类型是否存在
        /// </summary>
        /// <param name="accessoriestName">配件类型名称</param>
        /// <param name="id">配件类型ID</param>
        /// <returns></returns>

        public bool IsExist(string accessoriestName, int id = 0)
        {
            var expressionWhere = PredicateExtensions.True<AccessoriesType>();
            if (!string.IsNullOrWhiteSpace(accessoriestName))
                expressionWhere = expressionWhere.And(o => o.AccessoriesName.Equals(accessoriestName));
            if (id != 0)
                expressionWhere = expressionWhere.And(o => o.ID != id);
            return LoadEntities(expressionWhere).Any();
        }
        #endregion

        #region 分页获取配件类型列表 + IQueryable<AccessoriesType> GetPagingList(AccessoriesTypeModel model, out int total)
        /// <summary>
        /// 分页获取配件类型列表
        /// </summary>
        /// <param name="model">过滤条件</param>
        /// <param name="total">总数量</param>
        /// <returns></returns>

        public IQueryable<AccessoriesType> GetPagingList(AccessoriesTypeModel model, out int total)
        {
            var expressionWhere = PredicateExtensions.True<AccessoriesType>();

            //配件类型名称
            if (model.ProductTypeID>0)
            {
                expressionWhere = expressionWhere.And(o => o.ProductTypeID == model.ProductTypeID);
            }

            //配件类型名称
            if (!string.IsNullOrEmpty(model.AccessoriesName))
            {
                string kw = model.AccessoriesName.ToLower();
                expressionWhere = expressionWhere.And(o => o.AccessoriesName.ToLower().Contains(kw));
            }

            if (string.IsNullOrWhiteSpace(model.iSorts))
            {
                model.iSorts = "ID DESC";
            }
            return LoadPagerEntities<AccessoriesType>(model.PageIndex, model.iDisplayLength, out total, expressionWhere, model.iSorts);
        }
        #endregion
    }
}
